package com.deyuanyun.pic.ctrl.service.impl;

import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.converter.BeanConverter;
import com.deyuanyun.pic.ctrl.controller.dto.*;
import com.deyuanyun.pic.ctrl.enumeration.InterferenceEnum;
import com.deyuanyun.pic.ctrl.mapper.InfluenceMapper;
import com.deyuanyun.pic.ctrl.mapper.dao.AcdcDisturbMapper;
import com.deyuanyun.pic.ctrl.service.AcdcDisturbService;
import com.github.pagehelper.Page;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author Jerry
 * @Description
 * @date 2016-09-07
 */
@Service
public class AcdcDisturbServiceImpl implements AcdcDisturbService {

    @Resource
    private AcdcDisturbMapper acdcDisturbMapper;//交直流记录mapper

    @Resource
    private InfluenceMapper influenceMapper;//干扰源管理mapper


    @Cacheable(value = "tempStoreData")
    @Override
    public List<AcdcDisturbDTO> queryData(AcdcDisturbVO acdcDisturbVO) {
          return queryDataBydistubVo(acdcDisturbVO);
    }

    private List<AcdcDisturbDTO> queryDataBydistubVo(AcdcDisturbVO acdcDisturbVO){
        Page<AcdcDisturbDTO> result = new Page<AcdcDisturbDTO>();//申明最后返回的result 全局变量
        Page<AcdcDisturbDTO> list = acdcDisturbMapper.queryData(acdcDisturbVO);
        InfluenceVO vo = new InfluenceVO();
        vo.setBeginMileage(acdcDisturbVO.getBeginMileage());
        vo.setEndMileage(acdcDisturbVO.getEndMileage());
        vo.setPipeId(acdcDisturbVO.getPipeId());
        for (AcdcDisturbDTO acdcDisturbDTO : list) {
            if (acdcDisturbDTO.getIsId() != null) {
                vo.setInfluenceId(new String[]{acdcDisturbDTO.getIsId()});
            } else {
                vo.setInfluenceId(null);//覆盖各大管扰源的主键id
            }
            InterferenceEnum  interferenceType = InterferenceEnum.getEnum(acdcDisturbDTO.getIsType());
            switch (interferenceType) {
                case THVACTS:
                    List<InfluenceHtacDTO> htacList = influenceMapper.queryHtacData(vo);
                    transResult(htacList, acdcDisturbDTO, result, vo);
                    break;
                case ACERS:
                    List<InfluenceAcerDTO> acerList = influenceMapper.queryAcerData(vo);
                    transResult(acerList, acdcDisturbDTO, result, vo);
                    break;
                case HVDCTS:
                    List<InfluenceDcerDTO> dcerList = influenceMapper.queryDcerData(vo);
                    transResult(dcerList, acdcDisturbDTO, result, vo);
                    break;
                case DCTS:
                    List<InfluenceActowDTO> actowList = influenceMapper.queryActowData(vo);
                    transResult(actowList, acdcDisturbDTO, result, vo);
                    break;
                case OCPS:
                    List<InfluenceCpOtherDTO> cpotherList = influenceMapper.queryCpOtherData(vo);
                    transResult(cpotherList, acdcDisturbDTO, result, vo);
                    break;
                case ODEF:
                    List<InfluenceDcotherDTO> dcotherList = influenceMapper.queryDcotherData(vo);
                    transResult(dcotherList, acdcDisturbDTO, result, vo);
                    break;
            }

        }
          result.setTotal(list.getTotal());

        return result;
    }



    /**
     * 转换拼装数据
     *
     * @param listObj        根据istype查询对应干扰源的结果集
     * @param acdcDisturbDTO 原始结果集
     * @param result         申明要返回的result
     * @return
     */
    private void transResult(List<?> listObj, AcdcDisturbDTO acdcDisturbDTO, List<AcdcDisturbDTO> result, InfluenceVO vo) {
        Map map = null;
        List<Map<String, Object>> map1 = null;
        String pipeId = null;
        try {
            if (ObjectUtil.isNotNullEmpty(listObj)) {
                Object o = listObj.get(0);
                map = BeanConverter.toMap(o);
                pipeId = vo.getPipeId();
                if (StringUtils.isEmpty(pipeId)) {//初始化不选管道的时候默认数据本身的管道id
                    pipeId = (String) map.get("pipeLineId");
                }
                map1 = acdcDisturbMapper.queryGisIdByPipeId(pipeId);

                acdcDisturbDTO.setPipeName((String) map.get("pipeName"));
                acdcDisturbDTO.setGisId((String) map.get("gisid"));
                acdcDisturbDTO.setPipeGisId((String) map.get("pipeGisId"));
                acdcDisturbDTO.setFromName((String) map.get("beginMileage"));
                acdcDisturbDTO.setToName((String) map.get("endMileage"));
                acdcDisturbDTO.setMileage((String) map.get("crossMileage"));

                if (ObjectUtil.isNotNullEmpty(map1)) {
                    StringBuffer sb = new StringBuffer();
                    for (Map map2 : map1) {
                        sb.append(map2.get("valRoomGisId"));
                        sb.append("#");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    acdcDisturbDTO.setValRoomGisId(sb.toString());
                }
                result.add(acdcDisturbDTO);
            }

        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }


}
